﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Reflection;
namespace Z2.Core
{

    /// <summary>
    /// Represents static light.<br/>
    /// This light is not affected by camera position.
    /// </summary>
	public class StaticLight : IDraw
    {

        #region Const

        private const string DefaultLight = "light";

        #endregion

        #region Members

        private Light m_light;
		private Vector2 m_position;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public StaticLight()
		{
			this.m_light = new Light(StaticLight.DefaultLight);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets light position
        /// </summary>
        public Vector2 Position
		{
			get { return this.m_position; }
			set { this.m_position = value; }
		}

        /// <summary>
        /// Gets or sets ligth size
        /// </summary>
		public Rectangle Size
		{
			get { return this.m_light.LightSize; }
			set { this.m_light.LightSize = value; }
		}

        /// <summary>
        /// Gets or sets <see cref="Light"/>
        /// </summary>
		public Light Light
		{
			get { return this.m_light; }
			set { this.m_light = value; }
        }

        #endregion

        #region Methods

        public virtual void Update()
		{
			Vector2 v = new Vector2(
                (this.Position.X - Camera.Instance.Position.X) + (Light.LightSize.Width / 2),
                (this.Position.Y - Camera.Instance.Position.Y) + (Light.LightSize.Height / 2));
			int updateTo = (World.Current.ContainsItem(v) ? 0 : 255);
			if (this.Light.Color.A != updateTo)
			{
				int A = (byte)this.Light.Color.A + ((this.Light.Color.A > updateTo) ? -32 : 32);
				this.Light.Color = new Color(Color.White, Convert.ToByte(A < 0 ? 0 : (A > 255 ? 255 : A)));
			}
		}

		public virtual void DrawLight(SpriteBatch batch)
		{
			this.m_light.DrawLight(batch, this.Position);
		}

		public void Draw(SpriteBatch batch)
		{
			throw new NotImplementedException(MethodBase.GetCurrentMethod().Name);
        }

        #endregion

    }
}
